package pc.system;

import java.util.*;
import java.sql.*;
import pc.sql.*;

public class Actions
{
  public boolean new_version = true;

  private String language = "us";
  private Actions owner = null;
  private ConnectionPool connPool = null;
  private SQLCommand sqlCommand = null;
  private Filter filter = null;

  private Hashtable sorted_fields = null;
  private Request   request = null;
  private Response  response = null;
  private QueryString querystring = null;
  private SysLog    syslog = null;
  //
  private int permission = 0;

  public Actions(ConnectionPool connPool)
  {
    this.connPool      = connPool;
    this.sqlCommand    = new SQLCommand(this);
    this.filter        = new Filter(this);
    this.request       = new Request();
    this.response      = new Response(this);
    this.querystring   = new QueryString();
    this.syslog        = new SysLog(this);
    this.sorted_fields = new Hashtable();
  }

  public Actions(Actions owner)
  {
    this.owner = owner;
    if (owner != null) {
      this.connPool    = owner.connPool;
      this.filter      = owner.filter;
      this.request     = owner.request;
      this.response    = owner.response;
      this.querystring = owner.querystring;
      this.syslog      = owner.syslog;
      this.sqlCommand  = owner.sqlCommand;
      this.language    = owner.language;
      this.new_version = owner.new_version;
      this.permission  = owner.permission;
      this.sorted_fields = owner.sorted_fields;
    }
  }

  protected void finalize()
  {

  }

  public void setPermission(int perm)
  {
    this.permission = perm;
  }

  public int getPermission()
  {
    return this.permission;
  }

  public String getLanguage()
  {
    return this.language;
  }

  public void setLanguage(String language)
  {
    if (language == null) language = "us";
    this.language = language;
  }

  public void printDebug(String s)
  {
    System.out.println(s);
  }

  public Connection getConnection()
  {
    return (getSQLCommand() != null) ? getSQLCommand().getConnection() : null;
  }

  public ConnectionPool getConnectionPool()
  {
    return this.connPool;
  }

  public Actions getOwner()
  {
    return this.owner;
  }

  public Actions getRoot()
  {
    Actions act = this;
    while (act.owner != null) {
      act = act.owner;
    }
    return act;
  }

  public SQLCommand getSQLCommand()
  {
    sqlCommand.setOwner(this);
    return sqlCommand;
  }

  public Filter getFilter()
  {
    return filter;
  }

  public Hashtable getSortFields()
  {
    return this.sorted_fields;
  }

  public Request getRequest()
  {
    return request;
  }

  public Response getResponse()
  {
    return response;
  }

  public QueryString getQueryString()
  {
    return querystring;
  }
  
  public SysLog getSysLog()
  {
    return syslog;
  }

  protected boolean isValid()
  {
    return true;
  }

  public String getColor(int status)
  {
    String color[] = {"","#5392DD","#000000"};
    if (status <= 0 || status > 2) status = 1;
    return color[status];
  }
}
